package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Client;
import org.csu.petserver.domain.ClientSignOn;
import org.csu.petserver.domain.Order;
import org.csu.petserver.persistence.ClientMapper;
import org.csu.petserver.persistence.ClientSignOnMapper;
import org.csu.petserver.persistence.OrderMapper;
import org.csu.petserver.service.ClientInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("ClientInfo")
@MapperScan("org.csu.demo.persistence")
public class ClientInfoImpl implements ClientInfo {
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private ClientSignOnMapper clientSignOnMapper;
    @Autowired
    private ClientSignOn clientSignOn;
    @Autowired
    private Order order;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<Client> searchClient(String keyword) {//模糊搜索，只搜索用户id中含有keyword的用户
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userid",keyword);
        List<Client> clients = clientMapper.selectList(queryWrapper);
        return clients;
    }

    @Override
    public List<Client> allClient() {
        List<Client> clients = clientMapper.selectList(null);
        return clients;
    }

    @Override
    public boolean deleteClient(Client client) {
        clientMapper.deleteById(client.getId());
        clientSignOnMapper.deleteById(client.getId());
        return true;
    }

    @Override
    public boolean resetClientPassword(Client client) {//重置用户的密码为123(确定按下重置按钮后直接将该用户的密码改为123)
        clientSignOn.setUsername(client.getId());
        clientSignOn.setPassword("123");
        clientSignOnMapper.updateById(clientSignOn);
        return true;
    }

    @Override
    public List<Order> findOrderByClient(Client client) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",client.getId());
        List<Order> orders = orderMapper.selectList(queryWrapper);
        return orders;
    }

    @Override
    public Client findClientById(String clientId){
        QueryWrapper<Client> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",clientId);
        return clientMapper.selectOne(queryWrapper);
    }
}
